<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/26
 * Time: 14:41
 */
namespace app\reserve\controller;


use think\Controller;
use think\Db;
use think\Request;
use app\reserve\model\UserAddress as UserAddressModel;

class Useraddress extends Controller {

    /**
     * Notes: 添加用户地址
     * Create Date: 2018/10/26
     * Create by :lijie
     */
    public function addUserAddress(Request $request){
        $userAddress = new UserAddressModel();
        // 验证
        $param = $request->post();
        $resVali = $this->validate($param,"UserAddressValidate.addUserAddress");
        if($resVali !==true){
            return apiResponse(201,$resVali,'');
        }
        // 添加  判断传过来的默认地址是否为空
        if(input('post.status')){
            $save['status'] = 0;
            $w['u_id']      = $param['u_id'];
            $w['status']    = array("NEQ",9);
            $userAddress->editAddress($save,$w);
            $param['status'] = 2;
        }
        $res = $userAddress->addAddress($param);
        if($res){
            return apiResponse(200,'添加成功',$res);
        }else {
            return apiResponse(201,'添加失败',$res);
        }
    }

    /**
     * Notes:  删除用户地址
     * Create Date: 2018/10/26
     * Create by :lijie
     */
    public function delUserAddress(Request $request){
        $userAddress = new UserAddressModel();
        // 验证
        $param = $request->post();
        $resVali = $this->validate($param,"UserAddressValidate.delUserAddress");
        if($resVali !==true){
            return apiResponse(201,$resVali,'');
        }
        $res = $userAddress->delAddress($param);
        if($res){
            return apiResponse(200,'删除成功',$res);
        }else {
            return apiResponse(201,'删除失败',$res);
        }
    }

    /**
     * Notes:  编辑用户地址
     * Create Date: 2018/10/26
     * Create by :lijie
     */
    public function editUserAddress(Request $request){
        $userAddress = new UserAddressModel();
        // 验证
        $param = $request->post();
        $resVali = $this->validate($param,"UserAddressValidate.editUserAddress");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        if(!empty(input('post.status'))){
            $userAddress->editAddress(['status'=>0],['status'=>2]);
        }
        $res = $userAddress->editAddress($param,$param['id']);
        if($res){
            return apiResponse(200,'编辑成功',$res);
        }else {
            return apiResponse(201,'编辑失败',$res);
        }
    }


    /**
     * Notes:  查询用户地址
     * Create Date: 2018/10/26
     * Create by :lijie
     */
    public function userAddressList(Request $request){
        $userAddress = new UserAddressModel();
        // 验证
        $param = $request->post();
        $resVali = $this->validate($param,"UserAddressValidate.userAddressList");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        // 是否有用户地址id
        if(input('post.id')){
            $w['id'] = input('post.id');
            empty($param['u_id']) ? "" : $w['u_id'] = $param['u_id'];
            $res = $userAddress->findAddress($w);
            if($res){
                return apiResponse(200,'获取成功',$res);
            }else {
                return apiResponse(201,'获取失败',$res);
            }
        }else {
            // 联系人筛选
            empty(input('post.linkman')) ? "" : $w['linkman'] = $param['linkman'];
            // 电话筛选
            empty(input('post.phone')) ? "" : $w['phone']     = array('LIKE','%'.input('post.phone').'%');
            // 用户筛选
            empty(input('post.u_id')) ? "" : $w['u_id'] = $param['u_id'];
            // 时间筛选
            if(!empty(input('post.start')) && !empty(input('post.end'))){
                $w['ctime'] = array("EGT",strtotime(input('post.start')));
                $w['ctime'] = array("ELT",strtotime(input('post.end')));
            }

            $w['status']    = array("NEQ",9);
            $data['list']   = $userAddress->selectAddress($w,$param['pagenum'],$param['listNum']);
            $data['total']  = $userAddress->where($w)->count();
            if($data){
                return apiResponse(200,'获取成功',$data);
            }else {
                return apiResponse(201,'获取失败',$data);
            }
        }
    }

    /**
     * Notes:  设置默认用户地址
     * Create Date: 2018/10/1
     * Create by :lijie
     */
    public function defaultUserAddress()
    {
        $id = input('post.id');
        if(empty($id)){
            return apiResponse(201,"id不能为空",'');
        }
        $userAddress = new UserAddressModel();
        // 先把所有默认地址设为正常
        $userAddress->editAddress(['status'=>0],['status'=>2]);
        // 设置默认地址
        $res = $userAddress->editAddress(['status'=>2],['id'=>$id]);
        if($res){
            return apiResponse(200,'编辑成功',$res);
        }else {
            return apiResponse(201,'编辑失败',$res);
        }
    }

    /**
     * Notes:  查找用户地址
     * Create Date: 2018/10/1
     * Create by :lijie
     */
    public function findDefault(Request $request)
    {
        $userAddress = new UserAddressModel();
        // 验证
        $param = $request->post();
        $resVali = $this->validate($param,"UserAddressValidate.userAddressList");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        //
        $where['u_id']      = $param['u_id'];
        $where['status']    = 2;
        $res = $userAddress->findAddress($where);
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,'获取失败',$res);
        }
    }
}